6cc33f
@@ -29,37 +29,31 @@
import java.util.regex.Pattern;
 import org.springframework.util.Assert;
 
 /**
- * Represents a URI template. An URI template is a URI-like String that contained variables
- * marked of in braces (<code>{</code>, <code>}</code>), which can be expanded to produce a URI.
- * <p>See {@link #expand(Map)}, {@link #expand(String[])}, and {@link #match(String)} for example usages.
+ * Represents a URI template. An URI template is a URI-like String that contained variables marked of in braces
+ * (<code>{</code>, <code>}</code>), which can be expanded to produce a URI. <p>See {@link #expand(Map)}, {@link
+ * #expand(String[])}, and {@link #match(String)} for example usages.
  *
  * @author Arjen Poutsma
- * @since 3.0
  * @see <a href="http://bitworking.org/projects/URI-Templates/">URI Templates</a>
  * @since 3.0
  */
 public class UriTemplate {
 
-	/**
-	 * Captures URI template variable names.
-	 */
+	/** Captures URI template variable names. */
 	private static final Pattern NAMES_PATTERN = Pattern.compile("\\{([^/]+?)\\}");
 
-	/**
-	 * Replaces template variables in the URI template.
-	 */
+	/** Replaces template variables in the URI template. */
 	private static final String VALUE_REGEX = "(.*)";
 
-
 	private final List<String> variableNames;
 
 	private final Pattern matchPattern;
 
 	private final String uriTemplate;
 
-
 	/**
 	 * Construct a new {@link UriTemplate} with the given URI String.
+	 *
 	 * @param uriTemplate the URI template string
 	 */
 	public UriTemplate(String uriTemplate) {
@@ -69,9 +63,9 @@
public class UriTemplate {
 		this.matchPattern = parser.getMatchPattern();
 	}
 
-
 	/**
 	 * Return the names of the variables in the template, in order.
+	 *
 	 * @return the template variable names
 	 */
 	public final List<String> getVariableNames() {
@@ -79,10 +73,8 @@
public class UriTemplate {
 	}
 
 	/**
-	 * Given the Map of variables, expands this template into a URI.
-	 * The Map keys represent variable names, the Map values variable values.
-	 * The order of variables is not significant.
-	 * <p>Example:
+	 * Given the Map of variables, expands this template into a URI. The Map keys represent variable names, the Map values
+	 * variable values. The order of variables is not significant. <p>Example:
 	 * <pre>
 	 * UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
 	 * Map&lt;String, String&gt; uriVariables = new HashMap&lt;String, String&gt;();
@@ -91,10 +83,11 @@
public class UriTemplate {
 	 * System.out.println(template.expand(uriVariables));
 	 * </pre>
 	 * will print: <blockquote><code>http://example.com/hotels/1/bookings/42</code></blockquote>
+	 *
 	 * @param uriVariables the map of URI variables
 	 * @return the expanded URI
-	 * @throws IllegalArgumentException if <code>uriVariables</code> is <code>null</code>;
-	 * or if it does not contain values for all the variable names
+	 * @throws IllegalArgumentException if <code>uriVariables</code> is <code>null</code>; or if it does not contain values
+	 * for all the variable names
 	 */
 	public URI expand(Map<String, String> uriVariables) {
 		Assert.notNull(uriVariables, "'uriVariables' must not be null");
@@ -110,25 +103,22 @@
public class UriTemplate {
 	}
 
 	/**
-	 * Given an array of variables, expand this template into a full URI.
-	 * The array represent variable values. The order of variables is significant.
-	 * <p>Example:
-	 * <pre class="code>
-	 * UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
-	 * System.out.println(template.expand("1", "42));
-	 * </pre>
-	 * will print: <blockquote><code>http://example.com/hotels/1/bookings/42</code></blockquote>
+	 * Given an array of variables, expand this template into a full URI. The array represent variable values. The order of
+	 * variables is significant. <p>Example: <pre class="code> UriTemplate template = new
+	 * UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); System.out.println(template.expand("1", "42));
+	 * </pre> will print: <blockquote><code>http://example.com/hotels/1/bookings/42</code></blockquote>
+	 *
 	 * @param uriVariableValues the array of URI variables
 	 * @return the expanded URI
-	 * @throws IllegalArgumentException if <code>uriVariables</code> is <code>null</code>;
-	 * or if it does not contain sufficient variables
+	 * @throws IllegalArgumentException if <code>uriVariables</code> is <code>null</code>; or if it does not contain
+	 * sufficient variables
 	 */
 	public URI expand(String... uriVariableValues) {
 		Assert.notNull(uriVariableValues, "'uriVariableValues' must not be null");
 		if (uriVariableValues.length != this.variableNames.size()) {
-			throw new IllegalArgumentException("Invalid amount of variables values in [" +
-					this.uriTemplate + "]: expected " + this.variableNames.size() +
-					"; got " + uriVariableValues.length);
+			throw new IllegalArgumentException(
+					"Invalid amount of variables values in [" + this.uriTemplate + "]: expected " +
+							this.variableNames.size() + "; got " + uriVariableValues.length);
 		}
 		Matcher matcher = NAMES_PATTERN.matcher(this.uriTemplate);
 		StringBuffer buffer = new StringBuffer();
@@ -143,6 +133,7 @@
public class UriTemplate {
 
 	/**
 	 * Indicate whether the given URI matches this template.
+	 *
 	 * @param uri the URI to match to
 	 * @return <code>true</code> if it matches; <code>false</code> otherwise
 	 */
@@ -155,14 +146,11 @@
public class UriTemplate {
 	}
 
 	/**
-	 * Match the given URI to a map of variable values. Keys in the returned map are
-	 * variable names, values are variable values, as occurred in the given URI.
-	 * <p>Example:
-	 * <pre class="code">
-	 * UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
-	 * System.out.println(template.match("http://example.com/hotels/1/bookings/42"));
-	 * </pre>
-	 * will print: <blockquote><code>{hotel=1, booking=42}</code></blockquote>
+	 * Match the given URI to a map of variable values. Keys in the returned map are variable names, values are variable
+	 * values, as occurred in the given URI. <p>Example: <pre class="code"> UriTemplate template = new
+	 * UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); System.out.println(template.match("http://example.com/hotels/1/bookings/42"));
+	 * </pre> will print: <blockquote><code>{hotel=1, booking=42}</code></blockquote>
+	 *
 	 * @param uri the URI to match to
 	 * @return a map of variable values
 	 */
@@ -185,30 +173,57 @@
public class UriTemplate {
 		return this.uriTemplate;
 	}
 
-
 	private static URI encodeUri(String uri) {
 		try {
-			int idx = uri.indexOf(':');
-			URI result;
-			if (idx != -1) {
-				String scheme = uri.substring(0, idx);
-				String ssp = uri.substring(idx + 1);
-				result = new URI(scheme, ssp, null);
+			int schemeIdx = uri.indexOf(':');
+			if (schemeIdx == -1) {
+				int queryIdx = uri.indexOf('?');
+				if (queryIdx == -1) {
+					int fragmentIdx = uri.indexOf('#');
+					if (fragmentIdx == -1) {
+						return new URI(null, null, uri, null);
+					}
+					else {
+						String path = uri.substring(0, fragmentIdx);
+						String fragment = uri.substring(fragmentIdx + 1);
+						return new URI(null, null, path, fragment);
+					}
+				}
+				else {
+					int fragmentIdx = uri.indexOf('#', queryIdx + 1);
+					if (fragmentIdx == -1) {
+						String path = uri.substring(0, queryIdx);
+						String query = uri.substring(queryIdx + 1);
+						return new URI(null, null, path, query, null);
+					}
+					else {
+						String path = uri.substring(0, queryIdx);
+						String query = uri.substring(queryIdx + 1, fragmentIdx);
+						String fragment = uri.substring(fragmentIdx + 1);
+						return new URI(null, null, path, query, fragment);
+					}
+				}
 			}
 			else {
-				result = new URI(null, null, uri, null);
+				int fragmentIdx = uri.indexOf('#', schemeIdx + 1);
+				String scheme = uri.substring(0, schemeIdx);
+				if (fragmentIdx == -1) {
+					String ssp = uri.substring(schemeIdx + 1);
+					return new URI(scheme, ssp, null);
+				}
+				else {
+					String ssp = uri.substring(schemeIdx + 1, fragmentIdx);
+					String fragment = uri.substring(fragmentIdx + 1);
+					return new URI(scheme, ssp, fragment);
+				}
 			}
-			return result;
 		}
 		catch (URISyntaxException ex) {
 			throw new IllegalArgumentException("Could not create URI from [" + uri + "]: " + ex);
 		}
 	}
 
-
-	/**
-	 * Static inner class to parse uri template strings into a matching regular expression.
-	 */
+	/** Static inner class to parse uri template strings into a matching regular expression. */
 	private static class Parser {
 
 		private final List<String> variableNames = new LinkedList<String>();
@@ -240,7 +255,6 @@
public class UriTemplate {
 			return Pattern.quote(result);
 		}
 
-
 		private List<String> getVariableNames() {
 			return Collections.unmodifiableList(this.variableNames);
 		}
